package com.augurit.common.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.augurit.common.base.dao.BaseDao;
import com.augurit.common.entity.AcApplication;
import com.augurit.common.system.convert.AcApplicationConverter;
import com.augurit.common.system.util.OrgUtils;
import com.augurit.common.system.web.org.form.AcApplicationForm;
import com.augurit.common.util.lang.CollectionUtils;

/**
 * 应用系统DAO类
 */
@Repository
public class AcApplicationDao extends BaseDao<AcApplication, Long> {

	@Autowired
	private AcMenuDao acMenuDao;
	
	public AcApplicationForm getForm(Long id){
		if(id != null){
			AcApplication entity = this.get(id);
			return AcApplicationConverter.convertToForm(entity);
		}else
			return null;
	}
	
	/**
	 * 保存应用系统Form对象
	 * @param form 应用系统Form对象
	 */
	public void save(AcApplicationForm form){
		
		if(form != null){
			AcApplication entity = null;
			
			//准备VO对象
			if(form != null && form.getAppId() != null){
				entity = this.get(form.getAppId());
			}else{
				entity = new AcApplication();
			}
			
			//属性值转换
			AcApplicationConverter.convertToEntity(form, entity);
			
			//保存
			super.save(entity);
			
			//回填ID属性值
			form.setAppId(entity.getAppId());
		}
	}
	
	/**
	 * 保存应用系统Form对象
	 * @param form 应用系统Form对象
	 */
	public void saveCascade(AcApplicationForm form){	
		if(form != null){
			AcApplication entity = null;
			
			//准备VO对象
			boolean isNewEntity = false;
			if(form != null && form.getAppId() != null){
				entity = this.get(form.getAppId());
			}else{
				entity = new AcApplication();
				isNewEntity = true;
			}
			
			//判断是否改变应用系统访问地址
			if(!isNewEntity && OrgUtils.isAppUrlChange(form, entity)){
				acMenuDao.changeAppUrl(entity, form.getAppUrl());
			}
			
			//保存
			AcApplicationConverter.convertToEntity(form, entity);
			super.save(entity);
			form.setAppId(entity.getAppId());
			
			
		}
	}
	
	/**
	 * 获取所有应用ID集合
	 * @return
	 */
	public Long[] getAllAppIds(){
		String hql = "select distinct ps.appId from AcApplication ps";
		List<Long> list = this.find(hql);
		return CollectionUtils.toLongArray(list);
	}

	/**
	 * 获取指定WEB服务器下的应用ID集合
	 * @param servId
	 * @return
	 */
	public Long[] getAppIdsByServId(Long servId){
		List list = partAppByServId(servId, "appId");
		return CollectionUtils.toLongArray(list);
	}
	
	/**
	 * 获取指定WEB服务器下的应用ENTITY对象列表
	 * @param servId
	 * @return
	 */
	public List<AcApplication> getAppByServId(Long servId){
		return partAppByServId(servId, null);
	}
	
	private List partAppByServId(Long servId, String entityProperty){
		String hql = "select " 
						+ (entityProperty != null && entityProperty.trim().length() > 0 ? "ps." + entityProperty : "ps") 
						+ " from AcApplication ps where ps.servId = ?";
		List values = new ArrayList();
		values.add(servId);
		
		return this.find(hql,values);
	}
	
	public void disAssociateWithAcServer(Long servId){
		List<AcApplication> list = getAppByServId(servId);
			
		if(list != null && list.size() > 0){
			for(AcApplication entity : list){
				String newAppUrl = "/" + entity.getAppCode();
				
				//修改菜单关联项
				acMenuDao.changeAppUrl(entity, newAppUrl);
				
				//修改应用关联项
				entity.setServId(null);
				entity.setAppUrl(newAppUrl);
				this.save(entity);
			}
		}
	}
}
